package com.xuecheng.test.freemarker.controller;

import com.xuecheng.framework.domain.cms.CmsConfig;
import com.xuecheng.test.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping("/freemarker")
@Controller // 注意RestController和Controller的区别
public class FreeMarkerController {

    @Resource
    private RestTemplate restTemplate;


    @GetMapping("/test1")
    public String test1(Map<String, Object> map) {
        map.put("name", "传智播客");

        Student stu1 = new Student();
        stu1.setName("小明");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);
        stu2.setBirthday(new Date());

        List<Student> friends = new ArrayList<>();
        friends.add(stu1);
        stu2.setFriends(friends);
        stu2.setBestFriend(stu1);

        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        map.put("stus", stus);
        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);

        map.put("stu1", stu1);
        map.put("stuMap", stuMap);

        return "test1";
    }

    @GetMapping("/banner")
    public String index_banner(Map<String, Object> map) {
        // 使用Rest Template请求模型数据
        ResponseEntity<Map> forEntity = restTemplate.getForEntity("http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f", Map.class);
        Map body = forEntity.getBody();
        map.putAll(body);
        return "index_banner";
    }
    @GetMapping("/courseview/{id}")
    public String course(@PathVariable("id")String id, Map<String,Object> map){
        ResponseEntity<Map> forEntity = restTemplate.getForEntity("http://localhost:31200/course/courseview/"+id, Map.class);
        Map body = forEntity.getBody();
        map.putAll(body);
        return "course";
    }
}
